﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using HYCodeBase.Math;
using HYCodeBase.XmlUtility;

namespace HYRevitCode.RevitDimensionUtility
{
	public static class RevitDimensionUtiltyMethod
	{
		private static bool IsOkDimensionType(DimensionType dimType, List<BuiltInParameter> lstBldInPara)
		{
			if (lstBldInPara == null)
			{
				return true;
			}
			foreach (BuiltInParameter builtInParameter in lstBldInPara)
			{
				Parameter parameter = dimType.get_Parameter(builtInParameter);
				if (parameter == null || !parameter.IsReadOnly)
				{
					return false;
				}
			}
			return true;
		}

		public static DimensionType GetDimensionType(Autodesk.Revit.DB.Document doc, DimensionTypeEnum inputEnum, List<BuiltInParameter> lstBldInPara = null)
		{
			string name = "Name";
			string strXmlValue = "Value";
			string b = "线性尺寸标注样式";
			string text = string.Empty;
			string xmlPath = string.Empty;
			if (inputEnum == DimensionTypeEnum.AxisDimension)
			{
				text = "HY-轴线标注";
			}
			else
			{
				text = "HY-尺寸标注";
			}
			xmlPath = Product.DataLocation + string.Format("\\{0}.xml", text);
			DimensionType dimensionType = null;
			IEnumerable<Element> source = new FilteredElementCollector(doc).OfClass(typeof(DimensionType)).ToElements();
			DimensionType dimensionType2 = null;
			IEnumerable<Element> enumerable = (from n in source
			where n.ParametersMap != null && n.ParametersMap.Size != 0
			select n).ToList<Element>();
			List<DimensionType> list = new List<DimensionType>();
			foreach (Element element in enumerable)
			{
				DimensionType dimensionType3 = element as DimensionType;
				if (dimensionType3.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME) != null && dimensionType3.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == b)
				{
					dimensionType2 = dimensionType3;
					if (element.Name == text)
					{
						dimensionType = dimensionType3;
					}
					if (RevitDimensionUtiltyMethod.IsOkDimensionType(dimensionType3, lstBldInPara))
					{
						list.Add(dimensionType3);
					}
				}
			}
			if (dimensionType != null)
			{
				return dimensionType;
			}
			if (list.Count > 0)
			{
				dimensionType = (list.First<DimensionType>().Duplicate(text) as DimensionType);
			}
			else
			{
				dimensionType = (dimensionType2.Duplicate(text) as DimensionType);
			}
			using (List<XmlElement>.Enumerator enumerator2 = new HYCodeBase.XmlUtility.XmlHelper(xmlPath, "Grid").GetElements().GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					XmlElement node = enumerator2.Current;
					IList<Parameter> parameters = dimensionType.GetParameters(node.GetAttribute(name));
					if (parameters.Count > 0 && parameters.First<Parameter>() != null)
					{
						if (node.GetAttribute(name) == "记号" || node.GetAttribute(name) == "中心线记号")
						{
							string[] array = node.GetAttribute(strXmlValue).Split(new char[]
							{
								' '
							});
							Autodesk.Revit.DB.ElementType elementType = doc.GetElement(parameters.First<Parameter>().AsElementId()) as Autodesk.Revit.DB.ElementType;
							if (elementType != null && elementType.Name.Equals(array[0]))
							{
								Parameter parameter = elementType.get_Parameter(BuiltInParameter.ARROW_TYPE);
								if (parameter != null && !parameter.IsReadOnly)
								{
									parameter.Set(array[0]);
								}
								parameter = elementType.get_Parameter(BuiltInParameter.ARROW_SIZE);
								if (parameter != null && !parameter.IsReadOnly)
								{
									parameter.Set(UnitUtility.MMToFeet(Convert.ToDouble(array[1].Substring(0, array[1].Length - 2))));
								}
								parameters.First<Parameter>().Set(elementType.Id);
								continue;
							}
							ICollection<Element> collection = new FilteredElementCollector(doc).OfClass(typeof(Autodesk.Revit.DB.ElementType)).ToElements();
							if (collection.Contains((Element ele) => ele.Name.Equals(node.GetAttribute(strXmlValue))))
							{
								IEnumerator<Element> enumerator3 = collection.GetEnumerator();
								{
									while (enumerator3.MoveNext())
									{
										Element element2 = enumerator3.Current;
										if (element2.Name.Equals(node.GetAttribute(strXmlValue)))
										{
											Parameter parameter2 = element2.get_Parameter(BuiltInParameter.ARROW_TYPE);
											if (parameter2 != null && !parameter2.IsReadOnly)
											{
												parameter2.Set(array[0]);
											}
											parameter2 = element2.get_Parameter(BuiltInParameter.ARROW_SIZE);
											if (parameter2 != null && !parameter2.IsReadOnly)
											{
												parameter2.Set(UnitUtility.MMToFeet(Convert.ToDouble(array[1].Substring(0, array[1].Length - 2))));
											}
											parameters.First<Parameter>().Set(element2.Id);
											break;
										}
									}
									continue;
								}
							}



							IEnumerator<Element> enumerator = collection.GetEnumerator();
							{
								while (enumerator.MoveNext())
								{
									Element element3 = enumerator.Current;
									if (element3.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME) != null && element3.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == "箭头" && element3.Name.Contains("对角线"))
									{
										if (element3.Name == node.GetAttribute(strXmlValue))
										{
											Parameter parameter3 = element3.get_Parameter(BuiltInParameter.ARROW_TYPE);
											if (parameter3 != null && !parameter3.IsReadOnly)
											{
												parameter3.Set(array[0]);
											}
											parameter3 = element3.get_Parameter(BuiltInParameter.ARROW_SIZE);
											if (parameter3 != null && !parameter3.IsReadOnly)
											{
												parameter3.Set(UnitUtility.MMToFeet(Convert.ToDouble(array[1].Substring(0, array[1].Length - 2))));
											}
											parameters.First<Parameter>().Set(element3.Id);
											break;
										}
										elementType = (element3 as Autodesk.Revit.DB.ElementType).Duplicate(node.GetAttribute(strXmlValue));
										if (elementType != null)
										{
											Parameter parameter4 = elementType.get_Parameter(BuiltInParameter.ARROW_TYPE);
											if (parameter4 != null && !parameter4.IsReadOnly)
											{
												parameter4.Set(array[0]);
											}
											parameter4 = elementType.get_Parameter(BuiltInParameter.ARROW_SIZE);
											if (parameter4 != null && !parameter4.IsReadOnly)
											{
												parameter4.Set(UnitUtility.MMToFeet(Convert.ToDouble(array[1].Substring(0, array[1].Length - 2))));
											}
											parameters.First<Parameter>().Set(elementType.Id);
											break;
										}
									}
								}
								continue;
							}
						}
						if (node.GetAttribute(name) == "颜色")
						{
							Parameter parameter5 = parameters.First<Parameter>();
							if (parameter5 != null && !parameter5.IsReadOnly)
							{
								parameter5.Set(Convert.ToInt32(node.GetAttribute(strXmlValue)));
							}
						}
						else
						{
							if (node.GetAttribute(name) == "中心线样式")
							{
								ICollection<Element> collection2 = new FilteredElementCollector(doc).OfClass(typeof(LinePatternElement)).ToElements();
								Parameter parameter6 = parameters.First<Parameter>();
								if (parameter6 == null || parameter6.IsReadOnly || !collection2.Contains((Element ele) => ele.Name.Equals(node.GetAttribute(strXmlValue))))
								{
									continue;
								}
								IEnumerator<Element> enumerator = collection2.GetEnumerator();
								{
									while (enumerator.MoveNext())
									{
										Element element4 = enumerator.Current;
										if (element4.Name.Equals(node.GetAttribute(strXmlValue)))
										{
											parameter6.Set(element4.Id);
											break;
										}
									}
									continue;
								}
							}
							Parameter parameter7 = parameters.First<Parameter>();
							if (parameter7 != null && !parameter7.IsReadOnly)
							{
                                switch ((int)parameters.First<Parameter>().StorageType)
								{
								case 1:
									parameter7.Set(Convert.ToInt32(node.GetAttribute(strXmlValue)));
									break;
								case 2:
									parameter7.Set(UnitUtility.MMToFeet(Convert.ToDouble(node.GetAttribute(strXmlValue))));
									break;
								case 3:
									parameter7.Set(node.GetAttribute(strXmlValue));
									break;
								default:
									parameter7.SetValueString(node.GetAttribute(strXmlValue));
									break;
								}
							}
						}
					}
				}
			}
			return dimensionType;
		}

		public static string GetFortText(List<string> textTypeNames, List<string> lstPerfectMatchText, List<string> lstContainText)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			foreach (string key in lstPerfectMatchText)
			{
				dictionary[key] = null;
			}
			string text = null;
			foreach (string text2 in textTypeNames)
			{
				if (dictionary.TryGetValue(text2, out text))
				{
					dictionary[text2] = text2;
				}
			}
			foreach (string text3 in lstPerfectMatchText)
			{
				foreach (KeyValuePair<string, string> keyValuePair in dictionary)
				{
					if (text3.CompareTo(keyValuePair.Key) == 0 && keyValuePair.Value != null)
					{
						return keyValuePair.Value;
					}
				}
			}
			List<string> list = new List<string>();
			list.AddRange(textTypeNames);
			List<string> collection = null;
			foreach (string strContainText in lstContainText)
			{
				if (RevitDimensionUtiltyMethod.GetTextNoteTypeContainName(list, strContainText, out collection))
				{
					list.Clear();
					list.AddRange(collection);
				}
			}
			if (list.Count > 0)
			{
				return list[0];
			}
			if (textTypeNames.Count > 0)
			{
				return textTypeNames[0];
			}
			return string.Empty;
		}

		public static bool GetTextNoteTypeContainName(List<string> textTypes, string strContainText, out List<string> textTypesNew)
		{
			textTypesNew = new List<string>();
			for (int i = 0; i < textTypes.Count; i++)
			{
				if (textTypes[i].Contains(strContainText))
				{
					textTypesNew.Add(textTypes[i]);
				}
			}
			return textTypesNew.Count > 0;
		}

		public static string GetFortText(List<TextNoteType> textTypes, List<string> lstPerfectMatchText, List<string> lstContainText)
		{
			Dictionary<string, TextElementType> dictionary = new Dictionary<string, TextElementType>();
			foreach (string key in lstPerfectMatchText)
			{
				dictionary[key] = null;
			}
			TextElementType textElementType = null;
			foreach (TextElementType textElementType2 in textTypes)
			{
				if (dictionary.TryGetValue(textElementType2.Name, out textElementType))
				{
					dictionary[textElementType2.Name] = textElementType2;
				}
			}
			foreach (string text in lstPerfectMatchText)
			{
				foreach (KeyValuePair<string, TextElementType> keyValuePair in dictionary)
				{
					if (text.CompareTo(keyValuePair.Key) == 0 && keyValuePair.Value != null)
					{
						return keyValuePair.Value.Name;
					}
				}
			}
			List<TextNoteType> list = new List<TextNoteType>();
			list.AddRange(textTypes);
			List<TextNoteType> collection = null;
			foreach (string strContainText in lstContainText)
			{
				if (RevitDimensionUtiltyMethod.GetTextNoteTypeContainName(list, strContainText, out collection))
				{
					list.Clear();
					list.AddRange(collection);
				}
			}
			if (list.Count > 0)
			{
				return list[0].Name;
			}
			if (textTypes.Count > 0)
			{
				return textTypes[0].Name;
			}
			return string.Empty;
		}

		public static bool GetTextNoteTypeContainName(List<TextNoteType> textTypes, string strContainText, out List<TextNoteType> textTypesNew)
		{
			textTypesNew = new List<TextNoteType>();
			for (int i = 0; i < textTypes.Count; i++)
			{
				if (textTypes[i].Name.Contains(strContainText))
				{
					textTypesNew.Add(textTypes[i]);
				}
			}
			return textTypesNew.Count > 0;
		}
	}
}
